﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using GnProjectAssist;
using GnProjectAssist.Create;

namespace GnProjectAssistWindow
{
	/// <summary>
	/// CreateClassWindow.xaml에 대한 상호 작용 논리
	/// </summary>
	public partial class CreateClassWindow : Window, IGnPerformCommand
	{
		public CreateClassWindow()
		{
			InitializeComponent();
		}

		public void Perform()
		{
			Nullable<bool> dialogResult = ShowDialog();
			if( dialogResult.HasValue && dialogResult.Value )
			{
				
			}
		}

		protected bool Create( Object createValue )
		{
			try
			{
				base.Create( createValue );

				GnCreateClassData createData = createValue as GnCreateClassData;
				if( createData == null )
					return false;


				Solution CurrentSolution = (Solution)Application.Solution;
				SelectedItems selectItems = Application.SelectedItems;
				SelectedItem selectItem = selectItems.Item( 1 );
				SetVCProject( ref selectItem );
				if( VCProjcet == null )
					return false;

				String addfilePath = VCProjcet.ProjectDirectory;
				if( !iFileAssist.RemoveTail( ref addfilePath, @"Win32\VC90" ) )
					return false;

				String classDataPath = AddintDataPath + "Class\\";
				String itemFullPath = null;
				String tempFile = null;

				List<String> itemList = new List<String>( 3 );
				if( createData.isInline )
				{
					itemFullPath = addfilePath + createData.inlineName;
					tempFile = classDataPath + "inline.inl";
					if( File.Exists( tempFile ) )
					{
						File.Copy( tempFile, itemFullPath );
						itemList.Add( itemFullPath );
					}
				}

				if( createData.isMakeCpp )
				{
					itemFullPath = addfilePath + createData.cppName;
					tempFile = classDataPath + "cpp.cpp";
					if( File.Exists( tempFile ) )
					{
						File.Copy( classDataPath + "cpp.cpp", itemFullPath );
						itemList.Add( itemFullPath );
					}
				}

				itemFullPath = addfilePath + createData.headerName;
				tempFile = classDataPath + "header.h";
				if( File.Exists( tempFile ) )
				{
					File.Copy( classDataPath + "header.h", itemFullPath );
					itemList.Add( itemFullPath );
				}

				iClassDocument changeFile = new iClassDocument();
				String fileName = iSystem.iFileAssist.RemoveExtension( createData.headerName );

				changeFile.AddDictionary( "$(ClassName)", createData.className );
				changeFile.AddDictionary( "$(ProjectName)", VCProjcet.Name );
				changeFile.AddDictionary( "$(ProjectNameUP)", VCProjcet.Name.ToUpper() );
				changeFile.AddDictionary( "$(FileName)", fileName );
				changeFile.AddDictionary( "$(FileNameUP)", fileName.ToUpper() );

				if( selectItem.ProjectItem != null && selectItem.ProjectItem.Kind == "{6BB5F8F0-4483-11D3-8BCF-00C04F8EC28C}" )
				{
					VCFilter filter = selectItem.ProjectItem.Object as VCFilter;
					foreach( String itemPath in itemList )
					{
						changeFile.StartChange( itemPath, createData );
						filter.AddFile( itemPath );
					}
				}
				else
				{
					foreach( String itemPath in itemList )
					{
						changeFile.StartChange( itemPath, createData );
						selectItem.Project.ProjectItems.AddFromFile( itemPath );
					}
				}
				VCProjcet.Save();
			}
			catch( System.Exception ex )
			{
				MessageBox.Show( "ERROR: " + ex.Message );
			}
			return true;
		}

	}
}
